package com.vpx.server.service.system.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vpx.server.bean.entity.SysDicType;
import com.vpx.server.bean.entity.SysDicValue;
import com.vpx.server.bean.enums.AbleDisableEnum;
import com.vpx.server.bean.info.system.SysDicValueInfo;
import com.vpx.server.bean.order.base.BaseIdOrder;
import com.vpx.server.bean.order.system.*;
import com.vpx.server.bean.result.system.SysDicValueListResult;
import com.vpx.server.bean.result.system.SysDicValuePageResult;
import com.vpx.server.bean.result.system.SysDicValueResult;
import com.vpx.server.framework.aop.ServiceHandler;
import com.vpx.server.framework.base.ResultMsg;
import com.vpx.server.framework.exception.BizException;
import com.vpx.server.framework.utils.DateUtils;
import com.vpx.server.mapper.system.SysDicTypeMapper;
import com.vpx.server.mapper.system.SysDicValueMapper;
import com.vpx.server.service.system.SysDicValueService;
import com.vpx.server.utils.UserLoginUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liuks
 * @title: vpx
 * @module: 数据字典
 * @description: 数据字典服务接口实现
 * @date 2021/09/20 18:33
 */
@Service
public class SysDicValueServiceImpl implements SysDicValueService {

    @Autowired
    private SysDicValueMapper sysDicValueMapper;

    @Autowired
    private SysDicTypeMapper sysDicTypeMapper;

    @Override
    @ServiceHandler(memo = "新增数据字典", isTransaction = true)
    public ResultMsg insert(SysDicValueOrder order) {
        int count = sysDicValueMapper.queryDicValueCountByCode(order.getCode(), order.getTypeId());
        if(count > 0){
            throw new BizException("编码已存在");
        }
        SysDicValue dicValue = new SysDicValue();
        dicValue.from(order);
        dicValue.setStatus(AbleDisableEnum.ABLE.code());
        dicValue.setCreateUserId(UserLoginUtils.getCurrentUser().getId());
        dicValue.setCreateTime(DateUtils.now());
        sysDicValueMapper.insert(dicValue);
        return new ResultMsg();
    }

    @Override
    @ServiceHandler(memo = "修改数据字典", isTransaction = true)
    public ResultMsg updateById(SysDicValueOrder order) {
        SysDicValue dicValue = sysDicValueMapper.querySysDicValueById(order.getId());
        if(dicValue == null){
            throw new BizException("未查询到数据字典");
        }
        if(checkCodeIsExists(order)){
            throw new BizException("编码已存在");
        }
        dicValue.from(order);
        sysDicValueMapper.updateById(dicValue);
        return new ResultMsg();
    }

    @Override
    @ServiceHandler(memo = "删除数据字典", isTransaction = true)
    public ResultMsg deleteById(BaseIdOrder order) {
        sysDicValueMapper.updateSysDicValueById(order.getId(),AbleDisableEnum.DISABLE.code());
        return new ResultMsg();
    }

    @Override
    @ServiceHandler(memo = "查询数据字典", isTransaction = true)
    public ResultMsg<SysDicValueResult> findById(BaseIdOrder order) {
        SysDicValue dicValue = sysDicValueMapper.querySysDicValueById(order.getId());
        if(dicValue == null){
            throw new BizException("未查询到数据字典");
        }
        SysDicValueResult result = new SysDicValueResult();
        result.from(dicValue);
        return ResultMsg.success(result);
    }

    @Override
    @ServiceHandler(memo = "分页查询数据字典", isTransaction = true)
    public ResultMsg<SysDicValuePageResult> pageQuery(SysDicValuePageOrder order) {
        if (order.getPageNum() != null && order.getPageSize() != null) {
            PageHelper.startPage(order.getPageNum(), order.getPageSize());
        }
        List<SysDicValueInfo> dicValueInfos = sysDicValueMapper.pageQuery(order);
        PageInfo<SysDicValueInfo> pageInfo = new PageInfo<SysDicValueInfo>(dicValueInfos);
        SysDicValuePageResult result = new SysDicValuePageResult();
        result.setPageInfo(pageInfo);
        return ResultMsg.success(result);
    }

    @Override
    @ServiceHandler(memo = "根据类型编码查询数据字典列表", isTransaction = true)
    public ResultMsg<SysDicValueListResult> findListByTypeCode(SysDicValueCodeOrder order) {
        SysDicType dicType = sysDicTypeMapper.queryDicTypeByCode(order.getCode());
        if(dicType == null){
            throw new BizException("未查询到数据字典类型");
        }
        List<SysDicValueInfo> list = sysDicValueMapper.queryDicValueListByTypeId(dicType.getId());
        SysDicValueListResult result = new SysDicValueListResult();
        result.setList(list);
        return ResultMsg.success(result);
    }

    @ServiceHandler(memo = "检查编码是否存在")
    public boolean checkCodeIsExists(SysDicValueOrder order){
        int count = sysDicValueMapper.queryDicValueCountByIdAndCode(order.getId(),order.getCode(),order.getTypeId());
        if(count == 1){
            return false;
        }
        int num = sysDicValueMapper.queryDicValueCountByCode(order.getCode(),order.getTypeId());
        if(num > 0){
            return true;
        }
        return false;
    }
}
